﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FindIt.DAL;
using MySql.Data.MySqlClient;

namespace FindIt.BLL
{
    public class User
    {
        public enum RoleType { SU, PU, WU };
        private string _firstName;
        private string _lastName;
        private string _email;
        private string _username;
        private string _password;
        private RoleType _role;

        #region Properties

        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }

        public string Username
        {
            get { return _username; }
            set { _username = value; }
        }

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        public RoleType Role
        {
            get { return _role; }
            set { _role = value; }
        }

        #endregion

        #region Constructors
        public User()
        {
        }

        public User(string firstName, string lastName, string username, string password, string email, RoleType role)
        {
            this._firstName = firstName;
            this._lastName = lastName;
            this._username = username;
            this._password = password;
            this._email = email;
            this._role = role;
        }

        #endregion

        public void Updateprofile(string firstName, string lastName, string email)
        {
        }

        public static User GetUserByUsername(string username)
        {
            UserDB db = new UserDB();
            MySqlDataReader dr = db.GetUserByUsername(username);

            User user = new User();

            while (dr.Read())
            {
                user._firstName = dr["FirstName"].ToString();
                user._lastName = dr["LastName"].ToString();
                user._username = dr["Username"].ToString();
                user._password = dr["Password"].ToString();
                user._email = dr["Email"].ToString();
                user._role = (RoleType)Enum.Parse(typeof(RoleType), dr["Role"].ToString());
            }

            return user;
        }

        public static RoleType GetRoleByUsername(string username)
        {
            UserDB db = new UserDB();
            RoleType role =  db.GetRoleByUsername(username);
            return role;
        }

        public bool AddUserToDB()
        {
            UserDB db = new UserDB();
            if (!db.CheckIfUserNameExists(this.Username))
            {
                db.AddUser(this);
                return true;
            }
            else
                return false;
        }

    }
}
